package flex.cc.complain.service.impl;

import java.lang.reflect.InvocationTargetException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import flex.cc.PublicUtil;
import flex.cc.complain.dao.ComplainTrackDao;
import flex.cc.complain.entity.ComplainTrackEntity;
import flex.cc.complain.service.ComplainTrackService;
import flex.cc.complain.vo.ComplainVo;
import lombok.extern.slf4j.Slf4j;

@Service("complainTrackService")
@Transactional(rollbackFor = Exception.class)
@Slf4j
public class ComplainTrackServiceImpl extends ServiceImpl<ComplainTrackDao, ComplainTrackEntity> implements ComplainTrackService {
	@Autowired
	private ComplainTrackDao complainTrackDao;
	
	@Override
	public List<ComplainVo> queryTrackInfoByTsnumber(ComplainVo vo) {
		List<ComplainTrackEntity> list = this.list(new QueryWrapper<ComplainTrackEntity>()
				                                                   .eq("tsnumber", vo.getTsnumber())
				                                                   .orderByDesc("inserttime")
				                                                   );
		List<ComplainVo> dataList = new ArrayList<ComplainVo>();
		if(PublicUtil.isNotEmpty(list)) {
			for(int i=0;i<list.size();i++) {
				try {
					ComplainTrackEntity entity = list.get(i);
					ComplainVo dataVo = new ComplainVo();
					if("0".equals(entity.getStatus())) {
						entity.setStatus("投诉登记");
					}else if("1".equals(entity.getStatus())) {
						entity.setStatus("跟进处理");
					}else if("2".equals(entity.getStatus())) {
						entity.setStatus("结案备注");
					}
					BeanUtils.copyProperties(dataVo, entity);
					dataVo.setSaveTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(dataVo.getInserttime()));
					dataList.add(dataVo);
				} catch (IllegalAccessException e) {
					e.printStackTrace();
				} catch (InvocationTargetException e) {
					e.printStackTrace();
				}
			}
		}
		return dataList;
	}

	@Override
	public IPage<ComplainVo> queryComplainTrackByPage(Map map) {
		IPage<ComplainTrackEntity> page = new Page<>(Integer.parseInt(map.get("pageNum").toString()),Integer.parseInt(map.get("pageSize").toString()));
		IPage<ComplainVo> queryComplainTrackByPage = complainTrackDao.queryComplainTrackByPage(page, map);
		return queryComplainTrackByPage;
	}

	@Override
	public Map<String,String> queryComplainTrackByTsnumber() {
		Map<String,String> returnMap = new ConcurrentHashMap<String, String>();
		List<ComplainVo> list = complainTrackDao.queryComplainTrackByTsnumber();
		if(PublicUtil.isNotEmpty(list)) {
			for(int i=0;i<list.size();i++) {
				ComplainVo vo = list.get(i);
				returnMap.put(vo.getTsnumber(), vo.getMemoinfo());
			}
		}
		return returnMap;
	}

}
